<template>
  <div class="ape-admin-container">
    <section class="ape-admin-layout" v-if="showSearch">
      <el-form ref="queryForm" :inline="true" :model="queryParams" size="small">
        <el-form-item label="表名">
          <el-input
            v-model="queryParams.tableName"
            placeholder="表名"
          ></el-input>
        </el-form-item>
        <el-form-item label="表描述">
          <el-input
            v-model="queryParams.tableDescribe"
            placeholder="表描述"
          ></el-input>
        </el-form-item>
        <el-form-item label="同步状态">
          <el-select v-model="queryParams.syncStatus" placeholder="活动区域">
            <el-option label="全部" :value="null"></el-option>
            <el-option label="已同步" :value="1"></el-option>
            <el-option label="未同步" :value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch" icon="el-icon-search">
            搜索
          </el-button>
          <el-button type="warning" @click="resetQuery" icon="el-icon-refresh">
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </section>
    <section
      class="ape-admin-layout"
      type="flex"
      align="space-between"
      position="top"
    >
      <el-row>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >
          新增
        </el-button>
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >
          修改
        </el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >
          删除
        </el-button>
      </el-row>
      <el-row>
        <el-tooltip
          effect="dark"
          :content="showSearch ? '隐藏搜索' : '显示搜索'"
          placement="top"
        >
          <el-button
            icon="el-icon-search"
            size="mini"
            circle
            @click="showSearch = !showSearch"
          ></el-button>
        </el-tooltip>
        <el-tooltip effect="dark" content="刷新" placement="top">
          <el-button icon="el-icon-refresh" size="mini" circle></el-button>
        </el-tooltip>
      </el-row>
    </section>
    <section class="ape-admin-layout" scroll>
      <el-table
        size="mini"
        v-loading="loading"
        element-loading-text="拼命加载中"
        height="100%"
        :data="tableData"
        tooltip-effect="dark"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center">
        </el-table-column>
        <el-table-column
          prop="tableType"
          label="表类型"
          sortable
          width="120"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="tableName"
          label="表名"
          sortable
          width="200"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="tableDescribe" label="表描述" align="center">
        </el-table-column>
        <el-table-column
          prop="version"
          label="版本"
          sortable
          width="100"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="syncStatus"
          label="同步状态"
          sortable
          width="200"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="updateTime"
          label="更新时间"
          width="200"
          align="center"
          sortable
        >
        </el-table-column>
        <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              icon="el-icon-edit"
              size="mini"
              @click="handleUpdate(scope.row)"
            >
              编辑
            </el-button>
            &nbsp;
            <el-dropdown size="mini" @command="handleMore">
              <el-button type="text" size="mini">
                更多<i class="el-icon-arrow-down"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                  icon="el-icon-view"
                  :command="{ lab: 1, val: scope.row }"
                >
                  预览
                </el-dropdown-item>
                <el-dropdown-item
                  icon="el-icon-refresh"
                  :command="{ lab: 2, val: scope.row }"
                >
                  同步
                </el-dropdown-item>
                <el-dropdown-item
                  icon="el-icon-download"
                  :command="{ lab: 3, val: scope.row }"
                >
                  生成代码
                </el-dropdown-item>
                <el-dropdown-item
                  icon="el-icon-delete"
                  divided
                  :command="{ lab: 4, val: scope.row }"
                >
                  删除
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </section>
    <section
      class="ape-admin-layout"
      type="flex"
      align="center"
      position="bottom"
    >
      <el-pagination
        v-show="total > 0"
        :current-page.sync="queryParams.pageNum"
        :page-size.sync="queryParams.pageSize"
        :total="total"
        :page-sizes="[10, 30, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="getList"
        @current-change="getList"
      >
      </el-pagination>
    </section>
    <!-- 添加或修改字典数据对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="表类型" prop="tableType">
          <el-input v-model="form.tableType" placeholder="请输入表类型" />
        </el-form-item>
        <el-form-item label="表名" prop="tableName">
          <el-input v-model="form.tableName" placeholder="请输入表名" />
        </el-form-item>
        <el-form-item label="表描述" prop="tableDescribe">
          <el-input v-model="form.tableDescribe" placeholder="请输入表描述" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 是否显示搜索条件
      showSearch: true,
      // 加载遮罩层
      loading: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tableName: null,
        tableDescribe: null,
        syncStatus: null,
      },
      // 总条数
      total: 20,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 表格数据
      tableData: [],
      // 表单参数
      form: {
        id: null,
        tableType: null,
        tableName: null,
        tableDescribe: null,
      },
      // 表单校验
      rules: {
        tableType: {
          validator: (rule, value, callback) => {
            if (!value || value.length < 5) {
              callback(new Error("表类型至少五位数"));
            } else {
              callback();
            }
          },
          trigger: "blur",
        },
        tableName: {
          validator: (rule, value, callback) => {
            if (!value || value.length < 5) {
              callback(new Error("表名至少五位数"));
            } else {
              callback();
            }
          },
          trigger: "blur",
        },
      },
    };
  },

  created() {
    this.getList();
  },

  mounted() {},

  methods: {
    /** 查询数据列表 */
    getList() {
      this.loading = true;
      setTimeout(() => {
        this.loading = false;
      }, 1000);
    },
    /** 搜索按钮操作 */
    handleSearch() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.tableName = null;
      this.queryParams.tableDescribe = null;
      this.queryParams.syncStatus = null;
      this.resetForm("queryForm");
      this.handleSearch();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加数据";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids[0];

      this.form = {};
      this.open = true;
      this.title = "修改数据";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            this.$message({
              type: "success",
              message: "修改成功",
            });
            console.log(this.form);
            this.open = false;
            this.getList();
          } else {
            this.$message({
              type: "success",
              message: "添加成功",
            });
            console.log(this.form);
            this.open = false;
            this.getList();
          }
        }
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.resetForm("form");
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        tableType: null,
        tableName: null,
        tableDescribe: null,
      };
    },
    /** 多选框选中数据 */
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$msgbox({
        title: "",
        message: "此操作将永久删除选中的数据, 是否继续?",
        showClose: false,
        confirmButtonText: "确定",
        showCancelButton: true,
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        console.log(ids);
        this.$message({
          type: "success",
          message: "删除成功!",
        });
      });
    },
    /** 更多按钮操作 */
    handleMore(e) {
      switch (e.lab) {
        case 1:
          break;
        case 2:
          break;
        case 3:
          break;
        case 4:
          this.handleDelete(e.val);
          break;
        default:
          return;
      }
    },
  },
};
</script>
